<!DOCTYPE html>
<html lang="zh" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('合同列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse" style=display:none>
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>名称：</label>
                            <input name="contractName" type="text"/>
                        </li>
                        <li>
                            <label>编号：</label>
                            <input name="contractNumber" type="text"/>
                        </li>
                        <li>
                            <label>公司名称：</label>
                            <input name="companyName" type="text"/>
                        </li>
                        <li>
                            <label>经办人：</label>
                            <input name="contractCustomerManager" type="text"/>
                        </li>
                        <li>
                            <label>经办人号码：</label>
                            <input name="operatorPhoneNumber" type="text"/>
                        </li>
                        <li class="select-time">
                            <label>起始日期：</label>
                            <input class="time-input" id="startTime" name="params[beginContractStartDate]"
                                   placeholder="开始时间"
                                   type="text"/>
                            <span>-</span>
                            <input class="time-input" id="endTime" name="params[endContractStartDate]"
                                   placeholder="结束时间"
                                   type="text"/>
                        </li>
                        <li>
                            <label>状态：</label>
                            <select name="contractStatus" th:with="type=${@dict.getType('wb_contract_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="workbench:contract:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="workbench:contract:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="workbench:contract:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-info" onclick="$.table.importExcel()">
                <i class="fa fa-upload"></i> 导入
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <span class="pull-left mt10" style="color: red; ">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </span>
        </div>
    </form>
</script>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('workbench:contract:edit')}]];
    var removeFlag = [[${@permission.hasPermi('workbench:contract:remove')}]];
    var contractStatusDictDataList = [[${@dict.getType('wb_contract_status')}]];
    var prefix = ctx + "workbench/contract";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            importUrl: prefix + "/importData",
            modalName: "合同",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'contractName',
                    title: '合同名称'
                },
                {
                    field: 'companyName',
                    title: '单位名称'
                },
                {
                    field: 'contractNumber',
                    title: '合同编号',
                    formatter(value, index, item) {
                        return (value === '' || value === null) ? '未填写' : value;
                    }
                },
                {
                    field: 'contractValue',
                    title: '合同价值'
                },
                {
                    field: 'deptName',
                    title: '所属部门'
                },
                {
                    field: 'phoneNumber',
                    title: '负责人号码'
                },
                {
                    field: 'contractCustomerManager',
                    title: '经办人'
                },
                {
                    field: 'operatorPhoneNumber',
                    title: '经办人号码'
                },
                {
                    field: 'contractStartDate',
                    title: '开始日期',
                    formatter: function (value, row, index) {
                        return changeDateFormat(value);
                    }
                },
                {
                    field: 'contractEndDate',
                    title: '结束日期',
                    formatter: function (value, row, index) {
                        return changeDateFormat(value);
                    }
                },
                {
                    title: '剩余月份',
                    formatter: function (value, row, index) {
                        let endDate = row.contractEndDate;
                        return getDifferenceMonths(endDate)
                    }
                },
                {
                    field: 'contractStatus',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(contractStatusDictDataList, value);
                    }
                },
                {
                    field: 'contractPurpose',
                    title: '用途'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    /**
     * 计算给定时间和现在差几个月
     * @param date1 date1
     * @returns {string} 相差月份
     */
    function getDifferenceMonths(date1) {

        let m1 = new Date(date1).getTime();
        let m2 = new Date().getTime();

        let months = (m1 - m2) / 1000 / 60 / 60 / 24 / 30;
        return months.toFixed(1)
    }

    function changeDateFormat(cellval) {
        if (cellval) {
            var date = new Date(cellval);
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            return date.getFullYear() + "-" + month + "-" + currentDate;
        }
        return cellval;
    }
</script>
</body>
</html>
